(function(){

    function buildDataPicker(){
        var cb = function(start, end, label) {
            $('#reportrange span').html(start.format('YYYY.MM.DD') + ' - ' + end.format('YYYY.MM.DD'));
            $('#J_dtype').val(5);
            $('#J_date').val(start.format('YYYY.MM.DD') + '/' + end.format('YYYY.MM.DD'));
            $('#J_form').submit();
        };

        var optionSet1 = {
            //startDate: moment().subtract(29, 'days'),
            //endDate: moment(),
            minDate: '01/01/2014',
            maxDate: new Date(),
            dateLimit: {
                days: 360
            },
            showDropdowns: true,
            showWeekNumbers: false,
            timePicker: false,
            timePickerIncrement: 1,
            timePicker12Hour: true,
            opens: 'right',
            buttonClasses: ['btn btn-default'],
            applyClass: 'btn-small btn-primary',
            cancelClass: 'btn-small',
            format: 'YYYY.MM.DD',
            separator: ' to ',
            locale: {
                applyLabel: '确定',
                cancelLabel: '取消',
                fromLabel: '开始时间',
                toLabel: '结束时间',
                daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                monthNames: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
                firstDay: 1
            }
        };
        //$('#reportrange span').html(moment().subtract(29, 'days').format('YYYY.MM.D') + ' - ' + moment().format('YYYY.MM.D'));
        $('#reportrange').daterangepicker(optionSet1, cb);
        $('#options1').click(function() {
            $('#reportrange').data('daterangepicker').setOptions(optionSet1, cb);
        });
        $('#destroy').click(function() {
            $('#reportrange').data('daterangepicker').remove();
        });
    }

    function _event() {
        buildDataPicker();

        $('#J_date_item button').on('click', function() {
            var val = $(this).attr('data-id');
            $('#J_dtype').val(val);
            $('#J_form').submit();
        });

        $('#J_radio .radioItem').on('change', function() {
            var val = $(this).val();
            $('#J_site').val(val);
            $('#J_form').submit();
        });
    }

    $(function(){
        _event();
    });
})();